#/bin/bash

sids=($*)
dst=${sids[0]}
count=${#sids[*]}

if ((count < 2 || count > 6)); then
   ./util/error.sh "check servers" "count wrong"
   exit 1
fi

dst_db_host=`./config/get.sh game.db_host_id $dst`
dst_cloud_sid=`./config/get.sh game.cloud_sid $dst`

for sid in ${sids[*]}; do
    title="check $sid"
    merged=`./mysql/query_valid_server_field.sh $sid merged`
    if [ -z $merged ]; then
        ./util/error.sh "$title" "invalid"
        exit 1
    fi

    db_host_id=`./config/get.sh game.db_host_id $sid`
    if [ "$dst_db_host" != "$db_host_id" ]; then
        ./util/error.sh "$title" "not same db host"
        exit 2
    fi

    same_count=`grep -ow $sid merge/merge_list.txt | wc -l`
    if ((same_count > 1)); then
        ./util/error.sh "$title" "repeat"
        exit 2
    fi

    cloud_sid=`./config/get.sh game.cloud_sid $sid`
    if [ "$dst_cloud_sid" != "$cloud_sid" ]; then
        ./util/error.sh "$title" "not same cloud"
    fi
done

./util/info.sh "check" "OK"
